package cjyyp.文件编程;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.URI;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 利用Files的API统计jdk目录的jar数量
 */
@Slf4j
public class 遍历目录文件 {
    static String jdkFilePath="C:\\Program Files\\Java\\jdk1.8.0_351";
    public static void main(String[] args) throws IOException {
        Path path = Paths.get(jdkFilePath);
        AtomicInteger result=new AtomicInteger(0);
        Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toFile().getName().contains(".jar")) {
                    result.addAndGet(1);
                }
                return super.visitFile(file, attrs);
            }
        });
        log.info("jar文件一共有[{}]个",result.toString());
    }
}
